package com.atmyc.yygh.hospital.controller;

import com.atmyc.yygh.common.result.Result;
import com.atmyc.yygh.common.utils.MD5;
import com.atmyc.yygh.hospital.service.HospitalSetService;
import com.atmyc.yygh.model.hosp.Hospital;
import com.atmyc.yygh.model.hosp.HospitalSet;
import com.atmyc.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospital/")
@CrossOrigin
public class HospitalSetController {

    //注入service
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "获取全部医院信息")
    //1查询医院设置表所有信息
    @GetMapping("findAll")
    public Result findAllHospitalSet(){
        //调用service
        List<HospitalSet> list = hospitalSetService.list();
        System.out.println(list);
        return Result.ok(list);
    }

    @ApiOperation(value = "根据id删除医院信息")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if(flag) return Result.ok();
        else return Result.fail();
    }

    //3条件查询带分页接口
    @ApiOperation(value = "条件查询")
    @PostMapping("findPage/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current, @PathVariable long limit,@RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        //创建page对象,传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current,limit);
        //构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(hospitalSetQueryVo.getHosname()))
            wrapper.like("hosname",hospitalSetQueryVo.getHosname()); //模糊查询
        if(!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode()))
            wrapper.eq("hoscode",hospitalSetQueryVo.getHoscode()); //正常查询

        //调用方法实现分页查询
        Page<HospitalSet> pageHospitalSet = hospitalSetService.page(page,wrapper);
        return Result.ok(pageHospitalSet);
    }

    //4添加医院设置
    @ApiOperation(value = "保存医院设置")
    @PostMapping("saveHospitalSet")
    public Result saveHospital(@RequestBody HospitalSet hospitalSet){
        //设置状态，0 不能使用，1 使用
        hospitalSet.setStatus(1);
        //签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        hospitalSet.setIsDeleted(0);
        //调用service
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

        hospitalSet.setCreateTime(new Date());// new Date()为获取当前系统时间
        hospitalSet.setUpdateTime(new Date());
        boolean save = hospitalSetService.save(hospitalSet);
        if(save) return Result.ok();
        else return Result.fail();
    }

    //5根据id获取医院设置
    @ApiOperation(value = "根据id查询医院信息")
    @GetMapping("getHospital/{id}")
    public Result getHospSetById(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    //6 修改医院设置
    @ApiOperation(value = "修改医院设置")
    @PostMapping("updateHospital")
    public Result updateHospital(@RequestBody HospitalSet hospitalSet){
        boolean r = hospitalSetService.updateById(hospitalSet);
        if(r) return Result.ok();
        return Result.fail();
    }
    //7 批量删除
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList){
        boolean flag = hospitalSetService.removeByIds(idList);
        if (flag) return Result.ok();
        return Result.fail();
    }
    //8 医院设置锁定和解锁
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,@PathVariable Integer status){
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //修改信息
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();

    }
}
